<template>
	<view>
		<u-cell-group>
			<u-cell icon="pushpin-fill" title="工号" :value="userInfo.no"></u-cell>
			<u-cell icon="account-fill" title="姓名" :value="userInfo.name"></u-cell>
		</u-cell-group>
		<u-divider text="生产任务详情" :dashed="true"></u-divider>
		<uni-table border stripe emptyText="暂无更多数据">
			<uni-tr>
				<uni-th align="center">操作</uni-th>
				<uni-th align="center">数量</uni-th>
				<uni-th align="center">订单号</uni-th>
				<uni-th align="center">款号</uni-th>
				<uni-th align="center">合同号</uni-th>
				<uni-th align="center">工序名称</uni-th>
				<uni-th align="center">条码</uni-th>
				<uni-th align="center">尺码</uni-th>
				<uni-th align="center">颜色</uni-th>
				<uni-th align="center">单价</uni-th>
				<uni-th align="center">总金额</uni-th>
				<uni-th align="center">开始时间</uni-th>
				<uni-th align="center">结束时间</uni-th>
				<uni-th align="center">用时时间(分)</uni-th>
			</uni-tr>
			<uni-tr v-for="(item, index) in tableData" :key="index">
				<uni-td align="center">
					<u-button v-if="!item.dendDate" size="mini" type="info" @click="completeTask(item)">
						<u-icon name="play-right"></u-icon>
						完成
					</u-button>
					<u-button v-else size="mini" type="info" @click="open(item)">
						<u-icon name="eye"></u-icon>
						查看
					</u-button>
				</uni-td>
				<uni-td align="center">
					<view  v-if="!item.dendDate">
						<u-input v-model="item.iqty" type="number" @input="handleInput(index, item.iqty)" />
					</view>
					<view v-else>
						{{item.iqty}}
					</view>
				</uni-td>
				<uni-td align="center">{{item.sorderNo}}</uni-td>
				<uni-td align="center">{{item.sstyleNo}}</uni-td>
				<uni-td align="center">{{item.scontractNo}}</uni-td>
				<uni-td align="center">{{item.sprocedureName}}</uni-td>
				<uni-td align="center">{{item.spackBarCode}}</uni-td>
				<uni-td align="center">{{item.ssizeName}}</uni-td>
				<uni-td align="center">{{item.scolorName}}</uni-td>
				<uni-td align="center">{{item.nprice}}</uni-td>
				<uni-td align="center">{{item.namount}}</uni-td>
				<uni-td align="center">{{item.dbeginDate}}</uni-td>
				<uni-td align="center">{{item.dendDate}}</uni-td>
				<uni-td align="center">{{item.ntime}}</uni-td>
			</uni-tr>
		</uni-table>
		<u-popup :show="show" :round="10" mode="bottom" @close="close">
				<u-cell-group>
					<u-cell
					    size="mini"
					    title="数量"
					    :value="itemData.iqty"
					></u-cell>
					<u-cell
					    size="mini"
					    title="订单号"
					    :value="itemData.sorderNo"
					></u-cell>
					<u-cell
					    size="mini"
					    title="款号"
					    :value="itemData.sstyleNo"
					></u-cell>
					<u-cell
					    size="mini"
					    title="工序名称"
					    :value="itemData.sprocedureName"
					></u-cell>
					<u-cell
					    size="mini"
					    title="单价"
					    :value="itemData.nprice"
					></u-cell>
					<u-cell
					    size="mini"
					    title="总金额"
					    :value="itemData.namount"
					></u-cell>
					<u-cell
					    size="mini"
					    title="开始时间"
					    :value="itemData.dbeginDate"
					></u-cell>
					<u-cell
					    size="mini"
					    title="结束时间"
					    :value="itemData.dendDate"
					></u-cell>
					<u-cell
					    size="mini"
					    title="用时时间"
					    :value="itemData.ntime"
					></u-cell>
				</u-cell-group>
		</u-popup>
		<uni-fab horizontal="right" vertical="bottom" @fabClick="add"></uni-fab>
	</view>
</template>

<script>
	import rfidOutput from "@/api/report/rfidOutput"
	export default {
		data() {
			return {
				show: false,
				userInfo: '',
				itemData: {
					iqty: 0,
				},
				tableData: []
			}
		},
		onShow() {
			uni.showLoading({
				title: '加载中'
			});
			uni.$off('refreshData');
			uni.$on('refreshData', () => {
				this.loadAgain();
			});
			setTimeout(() => {
				uni.hideLoading();
			}, 1000);
		},
		onLoad() {
			this.userInfo = uni.getStorageSync('userInfo');
			this.loadAgain();
		},
		methods: {
			add() {
				uni.navigateTo({
					url: "/pages/subB/reportWork/timeOutput/barcodeForm"
				});
			},
			loadAgain() {
				uni.showLoading({
					title: '加载中'
				});
				rfidOutput.getCurrentRfidOutput().then(data => {
					this.tableData = data
					uni.hideLoading();
				})
			},
			handleInput(index, value) {
				// 输入验证逻辑（如限制最小值/最大值）
				this.tableData[index].iqty = parseInt(value);
			},
			completeTask(item){
				if (item.iqty <= 0) {
				    uni.showToast({
				        title: '数量<=0，请重新输入',
				        icon: 'error'
				    });
				    return
				}
				// 构建请求参数
			    const params = {
				    spackBarCode: item.spackBarCode,
				    dbeginDate: item.dbeginDate,
				    dendDate: this.formatDateTime(new Date()),
				    stype: '1',
				    id: item.id,
				    iqty: item.iqty
				}
				 uni.showLoading({
				 	title: '保存中'
				 });
				 rfidOutput.saveRfidOutPut(params).then(data => {
					this.loadAgain();
				 }).catch(error => { 
					 uni.showToast({
					     title: '保存失败',
					     icon: 'error'
					 });
					 uni.hideLoading();
				 })
			},
			formatDateTime(date){
				const year = date.getFullYear()
				const month = String(date.getMonth() + 1).padStart(2, '0')
				const day = String(date.getDate()).padStart(2, '0')
				const hours = String(date.getHours()).padStart(2, '0')
				const minutes = String(date.getMinutes()).padStart(2, '0')
				const seconds = String(date.getSeconds()).padStart(2, '0')		
				return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
			},	
			open(item){
				console.log(1)
				this.itemData = item;
				this.show = true;
			},
			close() {
			    this.show = false;
			}
		}
	}
</script>

<style>

</style>